import config from '../../../config';
import { Citizen } from '../../Citizen';
import { Zone } from '../zones/Zone';
import { Development } from './Development';
import { SimModule } from './SimModule';

export class Job extends SimModule {

    zone: Zone;

    workers: Citizen[] = [];

    constructor(zone: Zone) {
        super();
        this.zone = zone;
    }

    /**
     * Maximum number of workers that can work at this building
     */
    get maxWorkers() {
        // If building is not developed, there are no available jobs
        if (this.zone.development.state !== Development.state.developed) {
            return 0;
        } else {
            return Math.pow(config.modules.jobs.maxWorkers, this.zone.development.level);
        }
    }

    /**
     * Returns the number of job openings
     */
    get availableJobs() {
        return this.maxWorkers - this.workers.length;
    }

    /**
     * Returns the number of positions that are filled
     */
    get filledJobs() {
        return this.workers.length;
    }

    /**
     * Steps the state of the zone forward in time by one simulation step
     */
    simulate() {
        // If building is abandoned, all workers are laid off and no
        // more workers are allowed to work here
        if (this.zone.development.state === Development.state.abandoned) {
            this.layOffWorkers();
        }
    }

    /**
     * Lay off all existing workers
     */
    private layOffWorkers() {
        for (const worker of this.workers) {
            worker.setWorkplace(null);
        }
        this.workers = [];
    }

    /**
     * Handles any clean up needed before a building is removed
     */
    dispose() {
        this.layOffWorkers();
    }

    /**
     * Returns an HTML representation of this object
     */
    toHTML() {
        let html = `<div class="info-heading">Workers (${this.filledJobs}/${this.maxWorkers})</div>`;

        html += '<ul class="info-citizen-list">';
        for (const worker of this.workers) {
            html += worker.toHTML();
        }
        html += '</ul>';

        return html;
    }
}